// 获取vite配置的环境变量
// const isDev = import.meta.env.MODE === 'development'
// const envFlag = import.meta.env.VITE_ENV
// console.log(isDev);

// 防抖函数
export function debounce<T extends (...args: any[]) => void>(func: T, delay: number = 1000) {
  let timer: number | undefined = undefined
  return (...args: Parameters<T>) => {
    if (timer) clearTimeout(timer)
    timer = setTimeout(() => {
      func(...args)
    }, delay)
  }
}
// 节流函数
export function throttle<T extends (...args: any[]) => void>(func: T, delay: number = 1000) {
  let lastTime: number = 0
  let timer: number | undefined = undefined
  return (...args: Parameters<T>) => {
    const nowTime = new Date().getTime()
    if (nowTime - lastTime > delay) {
      if (timer) clearTimeout(timer)
      func(...args)
      lastTime = nowTime
    } else {
      if (timer) clearTimeout(timer)
      timer = setTimeout(
        () => {
          func(...args)
        },
        delay - (nowTime - lastTime),
      )
    }
  }
}
export default { debounce, throttle }

// 冒泡排序
export const maopao = (arr: number[]) => {
  const array = [...arr]
  let isOk = true
  for (let i = 0; i < array.length - 1; i++) {
    for (let j = i + 1; j < array.length; j++) {
      if (array[i] > array[j]) {
        const temp = array[i]
        array[i] = array[j]
        array[j] = temp
        isOk = false
      }
    }
  }
  return array
}

// 数组去重
export const removeRepeat = (arr: number[]): number[] => {
  return Array.from(new Set(arr))
  // const set = new Set<number>()
  // arr.forEach((item) => {
  //   if (!set.has(item)) {
  //     set.add(item)
  //   }
  // })
  // return Array.from(set)
}
